import { Redis } from 'ioredis'
import { Pool } from 'generic-pool'

export const poolConnectionMap = new WeakMap<PoolConnection, Pool<PoolConnection>>()

/**
 * 由连接池分配的redis连接
 */
export class PoolConnection extends Redis {
    /**
     * 释放连接
     */
    async release() {
        const pool = poolConnectionMap.get(this)
        if (pool) {
            await pool.release(this)
        }
    }
}
